﻿using Microsoft.Extensions.Options;
using Spark.Utility;

namespace Spark.Config.Api.AppCode.Sms
{
    /// <summary>
    /// 云信使sms.cn
    /// </summary>
    public class YunXinShiSMS : ISms
    {
        private readonly IOptionsMonitor<SmsConfig> _options;

        public YunXinShiSMS(IOptionsMonitor<SmsConfig> options)
        {
            _options = options;
        }

        public bool Send(string phone, string content, string signName = "")
        {
            if (!_options.CurrentValue.IsDebug)
            {
                var formData = $"ac=send&uid={_options.CurrentValue.YunXinShiSMS.Uid}" +
                               $"&pwd={_options.CurrentValue.YunXinShiSMS.Pass}" +
                               $"&mobile={phone}" +
                               $"&content={content}";

                var response = HttpHelper.Post<SendResponse>(_options.CurrentValue.YunXinShiSMS.Url, formData).Result;

                if (response != null && response.stat == "100")
                {
                    return true;
                }
                return false;
            }
            return true;
        }

        public class SendResponse
        {
            /// <summary>
            ///100        发送成功
            ///101        验证失败
            ///102        短信不足
            ///103        操作失败
            ///104        非法字符
            ///105        内容过多
            ///106        号码过多
            ///107        频率过快
            ///108        号码内容空
            ///109        账号冻结
            ///112	     号码错误
            ///113	     定时出错
            ///116         禁止接口发送
            ///117         绑定IP不正确
            ///161         未添加短信模板
            ///162        模板格式不正确
            ///163        模板ID不正确
            ///164        全文模板不匹配
            /// </summary>
            public string stat { get; set; }

            public string message { get; set; }
        }
    }
}